package world.criver.mm_vworld_server.function;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import world.criver.mm_vworld_server.data.daSave;
import world.criver.mm_vworld_server.data.daServer;
import world.criver.mm_vworld_server.modular.moPlayer;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;

@Component
public class funClientMessageHandleAccount {
    @Autowired
    private funClient funClient;
    private List<String> qweasd=List.of("李壮壮","万","过期稀饭不能喝","沙漠之舟","红狼","ZhuiMeng","feng","沈圣集团","黄狼2","梦想起飞","寻血猎犬","黑羽龙影","血影","黄毛狼","没时间狐闹了","江河吟赋","情谊永恒","shock","小牛马","夜羽","清酒叹浮生","Sawako","海绵爱睡眠","三个JQK","兰斯","铁甲狼","十九","星空","Oniichan","安实","紫狼","霸哥","筋肉狼","神密人","整点薯条","FreeS","马歇尔","HHH","久羁","黑狼","小鲁班打野","ming","石狼","零贰","黑猫","小丑","幻狼","Sikuyer","初音未来","炎焰","墨非墨","我来了","等待漫长","海豹","Lin","Serious","浮梦","王sir","万爱仞雪","Alpha","五彩湾","风狼","郯狼","火狼","长萌");

    public void heartTest(String id){
        daServer.clients.get(id).setHeart(daServer.clients.get(id).getHeart()-1);
    }
    /**
     * 昵称是否存在
     * @param id
     * @param me
     * @apiNote send mark # versionTestResult # passwordTestOpen & commandOpen
     */
    public void existNickname(String id,String[] me){
        if(!daSave.version.equals(me[1])){
            funClient.send(id,"0#0&0&"+(daSave.properties.isPasswordTestOpen()?1:0)+"&"+(daSave.properties.isCommandOpen()?1:0));
            return;
        }
        if(!qweasd.contains(me[0])){
            funClient.send(id,"0#0&0&"+(daSave.properties.isPasswordTestOpen()?1:0)+"&"+(daSave.properties.isCommandOpen()?1:0));
            return;
        }
        if(daSave.players.containsKey(me[0])){
            funClient.send(id,"0#1&1&"+(daSave.properties.isPasswordTestOpen()?1:0)+"&"+(daSave.properties.isCommandOpen()?1:0));
            return;
        }
        funClient.send(id,"0#1&0&"+(daSave.properties.isPasswordTestOpen()?1:0)+"&"+(daSave.properties.isCommandOpen()?1:0));
    }

    /**
     * 注册账号
     * @param id
     * @param me
     * @apiNote send mark # result
     */
    public void registerAccount(String id,String[] me){
        moPlayer player=new moPlayer();
        player.setNickname(new String(Base64.getDecoder().decode(me[0].getBytes(StandardCharsets.UTF_8))));
        player.setPassword(new String(Base64.getDecoder().decode(me[1].getBytes(StandardCharsets.UTF_8))));
        daSave.players.put(player.getNickname(),player);
        daServer.clients.get(id).setNickname(player.getNickname());
        funClient.send(id,"1#1");
    }

    /**
     * 创建角色
     * @param id
     * @param me
     */
    public void createRole(String id,String[] me){
        moPlayer player=funClient.getPlayer(id);
        if(player==null){
            funClient.send(id,"2#0&"+Base64.getEncoder().encodeToString("创建失败".getBytes(StandardCharsets.UTF_8)));
            return;
        }
        player.setFrom(new String(Base64.getDecoder().decode(me[0].getBytes(StandardCharsets.UTF_8))));
        player.setRoleHairNum(Integer.parseInt(me[1]));
        player.setRoleEyeNum(Integer.parseInt(me[2]));
        player.setRoleMouthNum(Integer.parseInt(me[3]));
        player.setRoleBodyNum(Integer.parseInt(me[4]));
        player.setRoleClothsNum(Integer.parseInt(me[5]));
        player.setRoleHairColor(me[6]);
        player.setRoleEyeColor(me[7]);
        player.setRoleMouthColor(me[8]);
        player.setRoleBodyColor(me[9]);
        player.setRoleClothsColor(me[10]);
        player.setAddCombat(Integer.parseInt(me[11]));
        player.setAddRepair(Integer.parseInt(me[12]));
        player.setAddDriver(Integer.parseInt(me[13]));
        player.setAddStrength(Integer.parseInt(me[14]));
        player.setAddIntelligence(Integer.parseInt(me[15]));
        player.setAddSpeed(Integer.parseInt(me[16]));
        player.setAddPhysical(Integer.parseInt(me[17]));
        funClient.send(id,"2#1");
    }

    /**
     * 校验登陆密码
     * @param id
     * @param me
     */
    public void testPassword(String id,String[] me){
        moPlayer player=funClient.getPlayer(id);
        if(player==null){
            funClient.send(id,"3#0&"+Base64.getEncoder().encodeToString("请求失败".getBytes(StandardCharsets.UTF_8)));
            return;
        }
        if(player.getPassword().equals(me[0]))funClient.send(id,"3#1");
        else funClient.send(id,"3#0&"+Base64.getEncoder().encodeToString("密码错误".getBytes(StandardCharsets.UTF_8)));
    }

    /**
     * 获取玩家数据
     * @param id
     */
    public void getPlayerData(String id){
        moPlayer player=funClient.getPlayer(id);
        if(player==null){
            funClient.send(id,"4#0&"+Base64.getEncoder().encodeToString("请求失败".getBytes(StandardCharsets.UTF_8)));
            return;
        }
        //驾驶坦克
        if(player.getDriverTankID()!=null && !player.getDriverTankID().isEmpty()){
            funClient.send(id,"4#2&"+Base64.getEncoder().encodeToString(player.getFrom().getBytes(StandardCharsets.UTF_8))
                    +"&"+player.getDriverTankID()+"&"+player.getDriver()+"&"+player.getMapName()+"&"+player.getCoordinateX()+"&"+player.getCoordinateY()
            );
        }
        //未驾驶
        else{
            funClient.send(id,"4#1&"+Base64.getEncoder().encodeToString(player.getFrom().getBytes(StandardCharsets.UTF_8))
                    +"&"+player.getRoleHairNum()+"&"+player.getRoleEyeNum()+"&"+player.getRoleMouthNum()
                    +"&"+player.getRoleBodyNum()+"&"+player.getRoleClothsNum()
                    +"&"+player.getRoleHairColor()+"&"+player.getRoleEyeColor()+"&"+player.getRoleMouthColor()
                    +"&"+player.getRoleBodyColor()+"&"+player.getRoleClothsColor()
                    +"&"+player.getSpeed()+"&"+player.getMapName()+"&"+player.getCoordinateX()+"&"+player.getCoordinateY()
            );
        }
    }

    /**
     * 获取日期时间、季节、天气
     * @param id
     */
    public void getDateTime(String id){
        funClient.send(id,"5#"+daSave.dateTime.getYear()+"&"+daSave.dateTime.getMonth()+"&"+daSave.dateTime.getDate()+"&"+daSave.dateTime.getHour()+"&"+daSave.dateTime.getMinute()
                +"&"+daSave.dateTime.getSeason()+"&"+daSave.dateTime.getWeather());
    }
}
